﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.IO;


namespace Modules
{
    public class CacheHttpModule : IHttpModule
    {
        public void Dispose()
        {
        }
        /// <summary>
        /// 应用程序资源第一次被访问时才执行一次，以后不会执行（Application对象)
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            // context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
            //     context.EndRequest += new EventHandler(context_EndRequest);
        }

        void context_EndRequest(object sender, EventArgs e)
        {
            var application = (HttpApplication)sender;
            application.Context.Response.Write("页面加载完毕");
        }

        /// <summary>
        /// 加载ＷＥＢ程序之前
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void context_BeginRequest(object sender, EventArgs e)
        {
            var application = (HttpApplication)sender;
            string path = application.Context.Request.PhysicalApplicationPath + "test.ashx";
            //   application.Context.Response.Write(VirtualPathUtility.GetExtension(application.Context.Request.FilePath).ToLower());
            //     application.Context.Response.Write(application.Context.Request.PhysicalPath);


            if (System.IO.File.Exists(path))
                System.IO.File.Delete(path);
            System.IO.File.AppendAllText(path, string.Format("<%@ Page Language=C# %><html><head></head><body>{0}</body></html>", "<%=DateTime.Now%>"));

            //using (StreamWriter write = new StreamWriter(path, false, System.Text.Encoding.UTF8))
            //{
            //    write.Write("");
            //}
            // application.Context.Response.WriteFile(path);
        }

        /// <summary>
        /// 加载ＷＥＢ程序之后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void context_ReleaseRequestState(object sender, EventArgs e)
        {
            var application = (HttpApplication)sender;
            application.Context.Response.Write(application.Context.Request.PhysicalPath);
        }
    }

    public class TestRemapHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello TestRemapHandler");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


    }

}
